package com.redis.server.cn.commen;
import com.redis.server.cn.config.ActionCodeConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by yunan on 2017/5/3.
 * 通用异常处理
 */

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Autowired
    private ActionCodeConfig actionCodeConfig;
    @ExceptionHandler(value = Exception.class)
    public ActionResult customerErrorHandler(HttpServletRequest req,Exception e){
        // 记录日志信息
        logger.error(e.getMessage());
        e.printStackTrace();
        // 返回通用数据结构 附加异常信息
        return new ActionResult(actionCodeConfig.getServerErrorCode(),String.format(actionCodeConfig.getServerErrorMessage(),e.getMessage()),null,req.getServletPath());
    }
}
